/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "file_文件名数据.h"

//#include <boost/filesystem.hpp>
#include <filesystem>


#ifdef _DEBUG

#pragma comment(lib, "libboost_locale-vc142-mt-sgd-x64-1_71.lib")

#else

#pragma comment(lib, "libboost_locale-vc142-mt-s-x64-1_71.lib")

#endif // DEBUG



bool f_file_文件是否存在(const std::string& path) {
	std::filesystem::path tmpPath(path);
	return std::filesystem::exists(tmpPath.string());
}

bool f_file_路径是否为目录(const std::string& path) {
	std::filesystem::path tmpPath(path);
	return std::filesystem::is_directory(tmpPath);
}

bool f_file_创建目录(const std::string& path) {
	return std::filesystem::create_directories(f_file_get路径(path));
	if (!f_file_文件是否存在(path)) {
		//boost::filesystem::create_directory(path);
		return true;
	}
	return false;
}

bool f_file_去路径空白字符(std::string& path) {
	uint32 num = path.size();
	for (uint32 i = 0; i < num; ) {
		if (i >= path.size()) break;
		if (path[i] == ' ') {
			path.erase(i, 1);
		}
		else {
			++i;
		}
	}
	return false;
}

std::string f_file_文件扩展名(const std::string& path) {
	std::filesystem::path tmpPath(path);
	return tmpPath.extension().string();
}

std::string f_file_取无后缀文件名(const std::string& path) {
	std::filesystem::path tmpPath(path);
	return tmpPath.stem().string();
}

std::wstring f_file_取无后缀文件名(const std::wstring& path) {
	std::filesystem::path tmpPath(path);
	return tmpPath.stem().wstring();
}

std::wstring f_file_取宽字符无后缀文件名(const std::string& path) {
	std::filesystem::path tmpPath(path);
	return tmpPath.stem().wstring();
}

std::string f_file_get路径(const std::string& fileName) {
	std::filesystem::path tmpPath(fileName);
	tmpPath = tmpPath.parent_path();
	//auto 去空格 = tmpPath.string();
	//uint32 num = 去空格.size();

	return tmpPath.string() + "/";
}

int32 f_file_get目录下所有文件(const std::string& dir, std::vector<std::string>& 目录文件名, std::string 扩展名顾虑) {
	std::filesystem::path path(dir);
	if (!std::filesystem::exists(path)) {
		return -1;
	}

	目录文件名.clear();

	std::filesystem::directory_iterator end_iter;
	for (std::filesystem::directory_iterator iter(path); iter != end_iter; ++iter) {
		if (std::filesystem::is_regular_file(iter->status())) {
			
			目录文件名.push_back(iter->path().string());
		}

		if (std::filesystem::is_directory(iter->status())) {
			目录文件名.push_back(iter->path().string());
		}
	}
	return 目录文件名.size();
}

/*
file_type 枚举类

	type : file_type
		none
		not_found
		regular
		directory
		symlink
		block
		character
		fifo
		socket
*/

/*

	permissions : perms
		none
		owner_read
		owner_write
		owner_exec
		owner_all
		group_read
		group_write
		group_exec
		group_all
		others_read
		others_write
		others_exec
		others_all
		all
		set_uid
		set_gid
		sticky_bit
		mask
*/
bool f_file_get目录下所有文件名(const std::string& dir, S_目录文件包& 目录文件, std::string 扩展名过滤) {
	auto ec = std::error_code();
	//std::filesystem::file_status status(t);
	auto it = std::filesystem::directory_iterator(dir, ec);
	if (ec.value() == 5) return false;

	//std::cout << " ec = " << ec << std::endl;
	for (; it != std::filesystem::directory_iterator(); ++it) {
		if ((*it).is_directory()) {
			目录文件.目录.push_back(it->path().filename().wstring());
		}
		else if(it->is_regular_file()) {
			if (扩展名过滤.empty() || it->path().extension() == 扩展名过滤) {
				目录文件.文件.push_back(it->path().filename().wstring());
			}
		}
		//std::cout << " it->status().permissions() = " << uint32(it->status().permissions()) << std::endl;
	}
	return true;
}

void f_file_get目录下所有文件名(const std::string& dir, S_目录文件包& 目录文件, const std::vector<std::string>& 扩展名过滤) {
	auto ec = std::error_code();
	//std::filesystem::file_status status(t);
	auto it = std::filesystem::directory_iterator(dir, ec);
	if (ec.value() == 5) return;

	//std::cout << " ec = " << ec << std::endl;
	for (; it != std::filesystem::directory_iterator(); ++it) {
		if ((*it).is_directory()) {
			目录文件.目录.push_back(it->path().filename().wstring());
		}
		else if (it->is_regular_file()) {
			for (auto& 过滤 : 扩展名过滤) {
				if (it->path().extension() == 过滤) {
					目录文件.文件.push_back(it->path().filename().wstring());
				}
			}
		}
	}
}

bool f_file_get目录下所有目录名(const std::string& dir, S_目录文件包& 目录文件) {
	auto ec = std::error_code();
	//std::filesystem::file_status status(t);
	auto it = std::filesystem::directory_iterator(dir, ec);
	if (ec.value() == 5) return false;

	for (; it != std::filesystem::directory_iterator(); ++it) {
		if ((*it).is_directory()) {
			目录文件.目录.push_back(it->path().filename().wstring());
		}
	}
	return true;
}
